package com.itheima.treemap;

import com.itheima.domain.Student;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * - 创建一个TreeMap集合,键是学生对象(Student),值是籍贯(String),学生属性姓名和年龄,按照年龄进行排序并遍历
 * - 要求按照学生的年龄进行排序,如果年龄相同则按照姓名进行排序
 */
public class Test01 {

    public static void main(String[] args) {
        //创建集合
        TreeMap<Student,String> map= new TreeMap<>();
        //创建学生对象
        Student s1= new Student("鹿晗",30);//太原
        Student s2= new Student("鹿晗",30);//北京
        Student s3= new Student("关晓彤",23);//北京
        Student s4= new Student("吴亦凡",30);//广州
        Student s5= new Student("蔡徐坤",22);//温州
        Student s6= new Student("黄子韬",27);//青岛

        //向集合中添加元素
        map.put(s1,"太原");
        map.put(s2,"北京");
        map.put(s3,"北京");
        map.put(s4,"广州");
        map.put(s5,"温州");
        map.put(s6,"青岛");

        //遍历集合
        //获取所有键
        Set<Student> students = map.keySet();
        for (Student student : students) {
            String jiaXiang = map.get(student);
            System.out.println(student+"========"+jiaXiang);
        }
        System.out.println("----------------------------------------");
        //通过键值对
        Set<Map.Entry<Student, String>> entries = map.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Student student = entry.getKey();
            String jiaXiang = entry.getValue();
            System.out.println(student+"========"+jiaXiang);
        }
        System.out.println("_>----------------------------------------");
        //foreach
        map.forEach((k,v)-> System.out.println(k+"==="+v));
    }
}
